package slices
{
	import display.Scene;
	
	import globals.$rootScene;
	
	import slices.TimeSlice;
	import globals.$athena;
	import slices.FunctionSlice;
	
	public class SwitchToScene extends TimeSlice
	{
		public var toScene:Scene; 
		
		public function SwitchToScene(s:Scene)
		{
			super();
			toScene = s;
		}
		
		override public function onStart():void {
			if ($rootScene.numChildren != 0) {				
				var fade:FadeOutDisplayObject = new FadeOutDisplayObject($rootScene, 0.1);
				fade.appendNext(new FunctionSlice(onFadeOut));
				$athena.addTimeSlice(fade);
			} else {
				onFadeOut();
			}
		}
		
		private function onFadeOut():void {
			$rootScene.removeChildren();
			$rootScene.addChild(toScene);
			$rootScene.alpha = 0;
			var fade:FadeInDisplayObject = new FadeInDisplayObject($rootScene, 0.1);
			fade.appendNext(new FunctionSlice(onFadeIn));
			$athena.addTimeSlice(fade);
		}
		
		private function onFadeIn():void {
			isDone = true;
		}
	}
}